package com.msb.push.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author: lmf
 * @Create: 2025/3/20 16:41
 * @module
 */
@Configuration
public class RabbitMQConfig {
    public static final String DELAYED_EXCHANGE = "push_delayed_exchange";

    public static final String DELAYED_QUEUE = "push_delayed_queue";

    private static final String DELAYED_EXCHANGE_TYPE = "x-delayed-message";

    private static final String DELAYED_ROUTING_TYPE_KEY = "x-delayed-type";

    private static final String DELAYED_ROUTING_TYPE_FANOUT = "fanout";

    /**
     * 创建延迟交换机
     */
    @Bean
    public Exchange delayedExchange() {
        HashMap<String, Object> args = new HashMap<>();
        // 使用这个延迟插件必须要有这个参数；值不一定但是key一定要有这个
        args.put(DELAYED_ROUTING_TYPE_KEY, DELAYED_ROUTING_TYPE_FANOUT);
        return new CustomExchange(DELAYED_EXCHANGE, DELAYED_EXCHANGE_TYPE, false, false, args);
    }

    /**
     * 创建延迟队列
     */
    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    /**
     * 绑定延迟队列到延迟交换机
     */
    @Bean
    public Binding delaydBinding(Exchange delayedExchange, Queue delayedQueue) {
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with("").noargs();
    }
}
